package com.leyou.item.service.impl;

import com.leyou.common.enums.ExceptionEnums;
import com.leyou.common.exception.LyException;
import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.mapper.SpecParamMapper;
import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import com.leyou.item.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SpecificationServiceImpl implements SpecificationService {

    @Autowired
    private SpecGroupMapper specGroupMapper;

    @Autowired
    private SpecParamMapper specParamMapper;


    @Override
    public List<SpecGroup> queryGroupByCid(Long cid) {

        SpecGroup specGroup = new SpecGroup();
        specGroup.setCid(cid);

        List<SpecGroup> specGroups = specGroupMapper.select(specGroup);
        if (CollectionUtils.isEmpty(specGroups)){
            throw new LyException(ExceptionEnums.SPEC_GROUP_NOT_FOND);
        }

        return specGroups;
    }

    /**
     * 新增规格组
     * @param specGroup
     */
    @Override
    public void saveGroup(SpecGroup specGroup) {
        int count = specGroupMapper.insert(specGroup);
        if (count!=1){
            throw new LyException(ExceptionEnums.GROUP_SAVE_ERROR);
        }
    }

    /**
     * 更新规格组
     * @param specGroup
     */
    @Override
    public void updateGroup(SpecGroup specGroup) {

        int count = specGroupMapper.updateByPrimaryKey(specGroup);
        if (count!=1){
            throw new LyException(ExceptionEnums.GROUP_UPDATE_ERROR);
        }

    }

    /**
     * 删除规格组
     * @param id
     */
    @Override
    public void deleteGroup(Long id) {

        int count = specGroupMapper.deleteByPrimaryKey(id);
        if (count!=1){
            throw new LyException(ExceptionEnums.DELETE_GROUP_ERROR);
        }
    }

    /**
     * 根据组查询参数
     * @param gid
     * @return
     */
    @Override
    public List<SpecParam> queryParamByGroupId(Long gid) {
        SpecParam specParam = new SpecParam();
        specParam.setGroupId(gid);

        List<SpecParam> specParams = specParamMapper.select(specParam);
        return specParams;
    }

    /**
     * 保存规格参数
     * @param specParam
     */
    @Override
    public void saveSpecParam(SpecParam specParam) {

        int count = specParamMapper.insert(specParam);

    }

    /**
     * 更新规格参数
     * @param specParam
     */
    @Override
    public void updateSpecParam(SpecParam specParam) {

       int count = specParamMapper.updateByPrimaryKey(specParam);
    }

    /**
     * 根据规格参数主键id删除规格参数
     * @param id
     */
    @Override
    public void deleteSpecParamById(Long id) {
        int count = specParamMapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据gid cid searching查询规格参数
     * @param gid
     * @param cid
     * @param searching
     * @return
     */
    @Override
    public List<SpecParam> queryParamList(Long gid, Long cid, Boolean searching) {

        SpecParam specParam = new SpecParam();
        specParam.setGroupId(gid);
        specParam.setCid(cid);
        specParam.setSearching(searching);

        List<SpecParam> specParams = specParamMapper.select(specParam);
        return specParams;
    }

    /**
     * 查询组和组下的参数
     * @param cid
     * @return
     */
    @Override
    public List<SpecGroup> queryListByCid(Long cid) {
        //查询组
        List<SpecGroup> specGroups = queryGroupByCid(cid);
        //查询分类下的参数
        List<SpecParam> specParams = queryParamList(null, cid, null);
        //把参数放进对应的组里
        Map<Long,List<SpecParam>> map = new HashMap();
        for (SpecParam specParam : specParams) {
            if (map.get(specParam.getGroupId())==null){
                map.put(specParam.getGroupId(),new ArrayList<>());
            }
            map.get(specParam.getGroupId()).add(specParam);
        }
        //把对应参数放进参数组
        for (SpecGroup specGroup : specGroups) {
            specGroup.setSpecParamList(map.get(specGroup.getId()));
        }

        return specGroups;
    }


}